package com.caul.demo.hadoop.hdfs.model;

import com.caul.demo.hadoop.hdfs.Cmd;

/**
 * Created by BlueDream on 2018-03-27.
 */
public class NameNodeReq {

  private String cmd = Cmd.UPLOAD;
  private String fileName;
  private long fileLen;

  public NameNodeReq(String cmd, String fileName, long fileLen) {
    this.cmd = cmd;
    this.fileName = fileName;
    this.fileLen = fileLen;
  }

  public NameNodeReq(String fileName, long fileLen) {
    this.fileName = fileName;
    this.fileLen = fileLen;
  }

  public String getCmd() {
    return cmd;
  }

  public void setCmd(String cmd) {
    this.cmd = cmd;
  }

  public String getFileName() {
    return fileName;
  }

  public void setFileName(String fileName) {
    this.fileName = fileName;
  }

  public long getFileLen() {
    return fileLen;
  }

  public void setFileLen(long fileLen) {
    this.fileLen = fileLen;
  }

  public String value() {
    return String.format(Cmd.NN_UPLOAD_REQ, getCmd(), getFileName(), getFileLen());
  }

  /**
   * @param str "cmd:fileName:fileLen"
   * @return
   */
  public static NameNodeReq of(String str) {
    if (str == null) {
      return null;
    }
    String[] blockAndNode = str.split(Cmd.SPLIT_CMD);
    String cmd = blockAndNode[0];
    if (Cmd.UPLOAD.equalsIgnoreCase(cmd)) {
      return new NameNodeReq(blockAndNode[1], Long.parseLong(blockAndNode[2]));
    } else if (Cmd.DOWNLOAD.equalsIgnoreCase(cmd)) {
      return new NameNodeReq(cmd, blockAndNode[1], 0);
    }
    return null;
  }
}
